/**
 * User: 夏汛
 * Date: 12-5-4
 * Time: 上午9:01
 */
Ext.extend(app.module,{
    currentFormValues: {},
    init: function(){
        var main = this;
        //用于列表
        this.ds = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({url:'score/getMarkUI.do'}),
            reader: new Ext.data.JsonReader({
                totalProperty: 'total',
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'cardid'},
                {name:'number'},
                {name:'username'},
                {name:'phone'},
                {name:'papername'},
                {name:'kemuname'},
                {name:'classname'},
                {name:'kgtmark'},
                {name:'zgtmark'},
                {name:'dztmark'},
                {name:'state'},
                {name:'firststarttime'}
            ])
        });
        // 翻页排序时带上查询条件
        this.ds.on('beforeload', function() {
            this.baseParams = {
                paperid : Ext.getCmp('queryPaper').getValue(),
                username : Ext.getCmp('queryStudentName').getValue(),
                classids : Ext.getCmp('queryClass').getValue()
            };
        });
        this.loader = new Ext.tree.TreeLoader({dataUrl:'class/getClassTree.do'});
        this.loader.processResponse = function(response, node, callback){
            var json = response.responseText;
            try{
                var json = eval("(" + json + ")");
                node.beginUpdate();
                var o = json["key"];
                for(var i=0,len=o.length; i<len; i++){
                    var n = this.createNode(o[i]);
                    if(n){
                        node.appendChild(n);
                    }
                }
                node.endUpdate();
                if(typeof callback == "function"){
                    callback(this,node);
                }
            } catch(e) {
                this.handleFailure(response);
            }
        }
        classTree = new Ext.tree.TreePanel( {
            loader: main.loader,
            root : {
                id: 0,
                text: '根节点',
                expanded: true
            },
            autoScroll : false,
            rootVisible : false,// 根节点是否可见
            animate : false,    // 展开,收缩动画
            useArrows : false,  // 是否使用箭头样式
            lines:false,        // 去掉树的线
            border : false
        });
        // 监听下拉树的节点单击事件
        classTree.on('click', function(node) {
            //递归获取所有子节点
            var getChild = function(n){
                if(!n.leaf){
                    var nodes = n.childNodes;
                    for(var i=0;i<nodes.length;i++){
                        classids.push(nodes[i].id);
                        getChild(nodes[i]);
                    }
                }
            }
            var classids = [];
            classids.push(node.id);
            getChild(node);
            main.comboxWithTree.setValue(node.text);
            Ext.getCmp('queryClass').setValue(classids);
            main.comboxWithTree.collapse();
        });
        this.comboxWithTree = new Ext.form.ComboBox({
            store : new Ext.data.SimpleStore( {
                fields : [],
                data : [[]]
            }),
            autoScroll : true,
            value : ' ',
            emptyText : '请选择...',
            mode : 'local',
            editable: false,
            triggerAction : 'all',
            maxHeight : 390,
            // 下拉框的显示模板,classTreeDiv作为显示下拉树的容器
            tpl : "<tpl for='.'><div style='height:390px'><div id='classTreeDiv'></div></div></tpl>",
            onSelect : Ext.emptyFn,
            onViewClick : Ext.emptyFn,
            assertValue : Ext.emptyFn
        });
        // 监听下拉框的下拉展开事件
        this.comboxWithTree.on('expand', function() {
            // 将树挂到treeDiv容器
            classTree.render('classTreeDiv');
        });

        var student_pagesize_combo = new Ext.form.ComboBox( {       //设置分页栏的每页记录数选择
            name : 'pagesize',
            hiddenName : 'pagesize',
            typeAhead : true,
            triggerAction : 'all',
            lazyRender : true,
            mode : 'local',
            store : new Ext.data.ArrayStore(
                {
                    fields : [ 'value', 'text' ],
                    data : [ [ 50, '50条/页' ], [ 100, '100条/页' ],
                        [ 200, '200条/页' ], [ 300, '300条/页' ],
                        [ 500, '500条/页' ], [ 2000, '2000条/页' ] ]
                }),
            valueField : 'value',
            displayField : 'text',
            value : '50',
            editable : false,
            width : 85
        });
        var number = parseInt(student_pagesize_combo.getValue());

        student_pagesize_combo.on("select", function(comboBox) {
            var bbar = Ext.getCmp('studentcjtjbbar');
            bbar.pageSize = parseInt(comboBox.getValue());
            number = parseInt(comboBox.getValue());
            main.ds.reload( {
                params : {
                    start : 0,
                    limit : bbar.pageSize
                }
            });
        });

        //试卷选择的store
        var paperStore = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({url:'paper/getNotEndPaper.do'}),
            reader: new Ext.data.JsonReader({
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'name'}
            ])
        });

        //试卷选择的下拉框
        comboPaper = new Ext.form.ComboBox({
            store: paperStore,
            editable: false,
            emptyText: '请选择试卷...',
            allowBlank: false,
            mode: 'remote',
            hiddenName : 'paperid',
            typeAhead : true,
            triggerAction : 'all',
            displayField: 'name',
            valueField: 'id',
            fieldLabel: '关联试卷'
        });

        comboPaper.on("select", function(comboBox) {
            Ext.getCmp('queryPaper').setValue(parseInt(comboBox.getValue()));
        });

        this.sm = new Ext.grid.CheckboxSelectionModel();
        this.body = new Ext.grid.GridPanel({
            store: this.ds,
            region: 'center',
            loadMask: true,
            viewConfig:{
                forceFit:true
            },
            sm: this.sm,
            columns: [
                new Ext.grid.RowNumberer(),
                this.sm,
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'cardid',
                    header: '身份证',
                    width: 180
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'number',
                    header: '学号',
                    width: 100
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'username',
                    header: '姓名',
                    width: 80
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'classname',
                    header: '班级',
                    width: 100
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'phone',
                    header: '电话号码',
                    width: 120
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'papername',
                    header: '试卷',
                    width: 80
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'kemuname',
                    header: '科目',
                    width: 80
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'kgtmark',
                    header: '客观题',
                    width: 50
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'state',
                    header: '状态',
                    width: 100,
                    renderer:function(value){
                        return value;
                    }
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'firststarttime',
                    header: '开始考试时间',
                    width: 100
                }
            ],
            tbar: {
                xtype: 'toolbar',
                items: [
                    {
                        text: '导出',
                        iconCls : 'btn-new',
                        scope: this,
                        handler: this.exportExcel
                    },
                    '->',
                    {
                        xtype: 'textfield',
                        value: '',
                        id: 'queryPaper',
                        hidden: true
                    },
                    {
                        xtype: 'displayfield',
                        value: '查询条件：'
                    },
                    ' ',
                    {
                        xtype: 'displayfield',
                        value: '试卷：'
                    },
                    comboPaper,
                    ' ',
                    {
                        xtype: 'displayfield',
                        value: '班级：'
                    },
                    ' ',
                    this.comboxWithTree,
                    {
                        xtype: 'textfield',
                        id : 'queryClass',
                        hidden: true
                    },
                    ' ',
                    {
                        xtype: 'textfield',
                        id : 'queryStudentName',
                        name : 'queryStudentName',
                        emptyText : '请输入姓名',
                        enableKeyEvents : true,
                        listeners : {
                            specialkey : function(field, e) {
                                if (e.getKey() == Ext.EventObject.ENTER) {
                                    main.query();
                                }
                            }
                        },
                        width : 80
                    },
                    {
                        text : '查询',
                        iconCls : 'btn-search',
                        handler : function() {
                            main.query();
                        }
                    }

                ]
            },
            bbar: {
                id: 'studentcjtjbbar',
                xtype: 'paging',
                pageSize : number,
                store : this.ds,
                displayInfo : true,
                displayMsg : '显示{0}条到{1}条,共{2}条',
                emptyMsg : "没有符合条件的记录",
                items : [ '-', '&nbsp;&nbsp;', student_pagesize_combo ]
            }
        });
        this.main.add(this.body);
        this.main.doLayout();
        this.ds.load({
            params: {
                start:0,
                limit:50
            }
        });
    },
    exportExcel: function(){
        Ext.Ajax.request({
            method:'POST',
            url: 'score/exportMark.do',
            params: {},
            disableCaching: true,   //禁用缓存
            scope: this,
            success: function(response){
                var json = Ext.util.JSON.decode(response.responseText);
                if(!(json.path==undefined || json.path=='')){
                    window.location.href = json.path;
                } else {
                    Ext.Msg.alert('提示', json.info);
                }
            },
            failure: function(form, action) {
                var info = action.result.info;
                Ext.Msg.alert('提示', info);
            }
        });
    },
    query: function(){
        this.ds.load( {
            params : {
                start : 0,
                limit : Ext.getCmp('studentcjtjbbar').pageSize,
                paperid : Ext.getCmp('queryPaper').getValue(),
                username : Ext.getCmp('queryStudentName').getValue(),
                classids : Ext.getCmp('queryClass').getValue()
            }
        });
    }
});